//---------------------------------------------------------------------
// File: BizTalkEventLogInstaller.cs
// 
// Summary: Class used to install the Microsoft.Practices.ESB Event
//          source for event log entries
//
//
//---------------------------------------------------------------------
// This file is part of the Microsoft ESB Guidance for BizTalk
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// This source code is intended only as a supplement to Microsoft BizTalk
// Server 2006 R2 release and/or on-line documentation. See these other
// materials for detailed information regarding Microsoft code samples.
//
// THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
// KIND, WHETHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
// PURPOSE.
//---------------------------------------------------------------------

using System;
using System.Xml;
using System.Reflection;
using System.Diagnostics;
using System.Globalization;
using System.Resources;
using System.Threading;
using System.Configuration.Install;
using System.ComponentModel;
using System.Security.Permissions;
namespace Microsoft.Practices.ESB.Exception.Management
{
    /// <summary>
    /// This contains 2 constants that defined the 
    /// default Event Source to create, and event log to
    /// write messages to.  Used externally as well by the 
    /// BizTalkEventLogInstaller class.
    /// </summary>
    public static class BizTalkEventLog
    {
        public const string Source = "Microsoft.Practices.ESB";
        public const string Log = "Application";
    }
    /// <summary>
    /// This class is an installer for Event Sources.  Used by the
    /// EventLogger class to create the event source, if it does not
    /// already exist.  There are to constructors.
    /// </summary>
    [RunInstaller(true)]
    public class BizTalkEventLogInstaller : Installer
    {
        private EventLogInstaller myEventLogInstaller;

        /// <summary>
        /// Creates an EventLogInstaller with the sourece, log and 
        /// message file specified and adds it to the Installers 
        /// collection, creating the event source
        /// </summary>
        /// <param name="source">Event Source to create</param>
        /// <param name="log">Event log</param>
        /// <param name="messageFile">message file for event source</param>
        [EventLogPermission(SecurityAction.Demand,
            MachineName = ".",
           PermissionAccess = EventLogPermissionAccess.Write)]
        public BizTalkEventLogInstaller(string source, string log,
            string messageFile)
        {
            if (string.IsNullOrEmpty(source))
                throw new ArgumentNullException("source");
            if (string.IsNullOrEmpty(log))
                throw new ArgumentNullException("log");
            if (string.IsNullOrEmpty(messageFile))
                throw new ArgumentNullException("messageFile");

            //Create Instance of EventLogInstaller
            myEventLogInstaller = new EventLogInstaller();

            // Set the Source of Event Log, to be created.
            myEventLogInstaller.Source = source;

            // Set the Log that source is created in
            myEventLogInstaller.Log = log;

            // Set the message file
            myEventLogInstaller.MessageResourceFile = messageFile;

            // Add myEventLogInstaller to the Installers Collection.
            Installers.Add(myEventLogInstaller);
        }

        /// <summary>
        /// Creates an EventLogInstaller with the sourece, log and 
        /// message file specified and adds it to the Installers 
        /// collection, creating the event source
        /// </summary>
        /// </summary>
        [EventLogPermission(SecurityAction.Demand,
            MachineName = ".",
           PermissionAccess = EventLogPermissionAccess.Administer)]
        public BizTalkEventLogInstaller()
        {
            //Create Instance of EventLogInstaller
            myEventLogInstaller = new EventLogInstaller();
            
            // Set the Source of Event Log, to be created.
            myEventLogInstaller.Source = BizTalkEventLog.Source;

            // Set the Log that source is created in
            myEventLogInstaller.Log = BizTalkEventLog.Log;

            // Add myEventLogInstaller to the Installers Collection.
            Installers.Add(myEventLogInstaller);
        }
    }
}
